package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.OrderDetail;
import com.web.exiaodao.business.repository.OrderDetailRepository;
import com.web.exiaodao.business.repository.OrderRepository;
import com.web.exiaodao.remote.service.ApiOrderDetailService;
import com.web.exiaodao.remote.vo.OrderDetailVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2016-11-07 15:15:38
 * 类的说明：前端APIOrder_Detail接口实现类
 **/
@Service("ApiOrder_DetailService")
@Transactional(readOnly = false)
public class ApiOrderDetailServiceImpl extends BaseService<OrderDetail> implements ApiOrderDetailService {

    private static final Logger LOGGER = Logger.getLogger(ApiOrderDetailServiceImpl.class);

    @Autowired
    OrderRepository orderRepository;
    @Autowired
    OrderDetailRepository orderDetailRepository;

    @Override
    public XaResult<OrderDetailVo> findOne(Long tId) throws BusinessException {
        OrderDetail obj = orderDetailRepository.findByIdAndStatusNot(tId, XaConstant.Status.delete);
        XaResult<OrderDetailVo> xr = new XaResult<OrderDetailVo>();
        if (XaUtil.isNotEmpty(obj)) {
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), OrderDetailVo.class));
            xr.getObject().setId(obj.getId());
            LOGGER.info("查询单个实体----->OrderDetail");
        } else {
            xr.error(XaConstant.Message.object_not_find);
            LOGGER.info(XaConstant.Message.object_not_find);
        }
        return xr;
    }

    @Override
    public XaResult<OrderDetailVo> findLastOne() throws BusinessException {
        throw new  BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<List<OrderDetailVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示非删除的所有数据
            status = XaConstant.Status.delete;
        }
        filters.put("status", new SearchFilter("status", Operator.NE, status));
        Page<OrderDetail> page = orderDetailRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), OrderDetail.class), pageable);
        XaResult<List<OrderDetailVo>> xr = new XaResult<List<OrderDetailVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), OrderDetailVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<List<OrderDetailVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
        if (status == null) {// 默认显示正常数据
            status = XaConstant.Status.valid;
        }
        filters.put("status", new SearchFilter("status", Operator.EQ, status));
        Page<OrderDetail> page = orderDetailRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), OrderDetail.class), pageable);
        XaResult<List<OrderDetailVo>> xr = new XaResult<List<OrderDetailVo>>();
        xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), OrderDetailVo.class));
        for (int i = 0; i < page.getContent().size(); i++) {
            xr.getObject().get(i).setId(page.getContent().get(i).getId());
        }
        return xr;
    }

    @Override
    public XaResult<OrderDetailVo> multiOperate(String modelIds,
                                                Integer status) throws BusinessException {
        XaResult<OrderDetailVo> xr = new XaResult<OrderDetailVo>();
        if (status == null) {
            status = XaConstant.Status.delete;
        }
        if (modelIds != null) {
            String[] ids = modelIds.split(",");
            for (String id : ids) {
                OrderDetail obj = orderDetailRepository.findByIdAndStatusNot(Long.parseLong(id), status);
                if (XaUtil.isNotEmpty(obj)) {
                    obj.setStatus(status);
                    obj = orderDetailRepository.save(obj);
                    xr.setObject(JSON.parseObject(JSON.toJSONString(obj), OrderDetailVo.class));
                    xr.getObject().setId(obj.getId());
                }
            }
        }
        LOGGER.info("更改数据状态----->OrderDetail");
        return xr;
    }

    @Override
    public XaResult<OrderDetailVo> createModel(OrderDetail model)
            throws BusinessException {
        XaResult<OrderDetailVo> xr = new XaResult<OrderDetailVo>();
        OrderDetail obj = orderDetailRepository.save(model);
        xr.setObject(JSON.parseObject(JSON.toJSONString(obj), OrderDetailVo.class));
        xr.getObject().setId(obj.getId());
        LOGGER.info("新增数据成功----->OrderDetail");
        return xr;
    }

    @Override
    public XaResult<OrderDetailVo> updateModel(OrderDetail model)
            throws BusinessException {
        OrderDetail obj = orderDetailRepository.findOne(model.getId());
        XaResult<OrderDetailVo> xr = new XaResult<OrderDetailVo>();
        if (XaUtil.isNotEmpty(obj)) {
            obj.setOrderNo(XaUtil.isNotEmpty(model.getOrderNo()) ? model.getOrderNo() : obj.getOrderNo());
            obj.setShop_ProductSPU(XaUtil.isNotEmpty(model.getShop_ProductSPU()) ? model.getShop_ProductSPU() : obj.getShop_ProductSPU());
            obj.setStandardValues(XaUtil.isNotEmpty(model.getStandardValues()) ? model.getStandardValues() : obj.getStandardValues());
            obj.setProductNum(XaUtil.isNotEmpty(model.getProductNum()) ? model.getProductNum() : obj.getProductNum());
            obj.setShopName(XaUtil.isNotEmpty(model.getShopName()) ? model.getShopName() : obj.getShopName());
            obj.setComm(XaUtil.isNotEmpty(model.getComm()) ? model.getComm() : obj.getComm());
            obj.setProductOrderPrice(XaUtil.isNotEmpty(model.getProductOrderPrice()) ? model.getProductOrderPrice() : obj.getProductOrderPrice());
            obj.setShopOriginalPrice(XaUtil.isNotEmpty(model.getShopOriginalPrice()) ? model.getShopOriginalPrice() : obj.getShopOriginalPrice());
            obj = orderDetailRepository.save(obj);
            xr.setObject(JSON.parseObject(JSON.toJSONString(obj), OrderDetailVo.class));
            xr.getObject().setId(obj.getId());
        } else {
            xr.error(XaConstant.Message.object_not_find);
        }
        LOGGER.info("编辑数据成功----->OrderDetail");
        return xr;
    }

}
